# 备份恢复脚本
# 用途: 从备份目录恢复Form1相关文件

param(
    [string]$BackupDir = "",  # 备份目录名称，如 "Backup_20251024_090734"
    [switch]$ListBackups = $false,  # 列出所有可用的备份
    [switch]$Help = $false  # 显示帮助信息
)

# 项目路径
$ProjectDir = "d:\src\DotNet\demo\demo"

# 显示帮助信息
function Show-Help {
    Write-Host "备份恢复脚本使用说明:" -ForegroundColor Green
    Write-Host ""
    Write-Host "用法:" -ForegroundColor Yellow
    Write-Host "  .\RestoreBackup.ps1 -BackupDir <备份目录名>    # 恢复指定备份"
    Write-Host "  .\RestoreBackup.ps1 -ListBackups             # 列出所有可用备份"
    Write-Host "  .\RestoreBackup.ps1 -Help                    # 显示帮助信息"
    Write-Host ""
    Write-Host "示例:" -ForegroundColor Yellow
    Write-Host "  .\RestoreBackup.ps1 -BackupDir Backup_20251024_090734"
    Write-Host "  .\RestoreBackup.ps1 -ListBackups"
    Write-Host ""
}

# 列出所有可用的备份
function List-Backups {
    Write-Host "可用的备份目录:" -ForegroundColor Green
    Write-Host ""
    
    $backupDirs = Get-ChildItem -Directory -Path $ProjectDir -Name "Backup_*" | Sort-Object -Descending
    
    if ($backupDirs.Count -eq 0) {
        Write-Host "没有找到任何备份目录。" -ForegroundColor Red
        return
    }
    
    for ($i = 0; $i -lt $backupDirs.Count; $i++) {
        $dir = $backupDirs[$i]
        $dirInfo = Get-Item (Join-Path $ProjectDir $dir)
        $date = $dir.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
        
        Write-Host "[$($i+1)] $dir (创建时间: $date)" -ForegroundColor Cyan
    }
    
    Write-Host ""
    Write-Host "使用 .\RestoreBackup.ps1 -BackupDir <备份目录名> 来恢复指定的备份" -ForegroundColor Yellow
}

# 恢复备份
function Restore-Backup {
    param([string]$BackupName)
    
    $backupPath = Join-Path $ProjectDir $BackupName
    
    # 检查备份目录是否存在
    if (-not (Test-Path $backupPath -PathType Container)) {
        Write-Host "错误: 备份目录 '$BackupName' 不存在!" -ForegroundColor Red
        Write-Host "使用 -ListBackups 参数查看可用的备份目录。" -ForegroundColor Yellow
        return
    }
    
    # 检查备份文件是否存在
    $filesToRestore = @("Form1.cs", "Form1.Designer.cs", "Form1.resx")
    $missingFiles = @()
    
    foreach ($file in $filesToRestore) {
        $filePath = Join-Path $backupPath $file
        if (-not (Test-Path $filePath -PathType Leaf)) {
            $missingFiles += $file
        }
    }
    
    if ($missingFiles.Count -gt 0) {
        Write-Host "错误: 备份目录中缺少以下文件:" -ForegroundColor Red
        $missingFiles | ForEach-Object { Write-Host "  - $_" -ForegroundColor Red }
        return
    }
    
    # 创建当前文件的备份（以防恢复后需要再次回退）
    $currentBackup = "Backup_BeforeRestore_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
    $currentBackupPath = Join-Path $ProjectDir $currentBackup
    
    Write-Host "创建当前文件的备份到: $currentBackup" -ForegroundColor Green
    New-Item -Path $currentBackupPath -ItemType Directory -Force | Out-Null
    
    foreach ($file in $filesToRestore) {
        $sourceFile = Join-Path $ProjectDir $file
        if (Test-Path $sourceFile -PathType Leaf) {
            Copy-Item $sourceFile $currentBackupPath -Force
        }
    }
    
    # 恢复文件
    Write-Host "正在从备份恢复文件..." -ForegroundColor Green
    
    foreach ($file in $filesToRestore) {
        $sourceFile = Join-Path $backupPath $file
        $destFile = Join-Path $ProjectDir $file
        
        Copy-Item $sourceFile $destFile -Force
        Write-Host "已恢复: $file" -ForegroundColor Green
    }
    
    Write-Host ""
    Write-Host "备份恢复完成!" -ForegroundColor Green
    Write-Host "当前文件已备份到: $currentBackup" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "接下来需要重新编译项目:" -ForegroundColor Yellow
    Write-Host "& `"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe`" demo.csproj" -ForegroundColor Gray
}

# 主逻辑
if ($Help) {
    Show-Help
    exit
}

if ($ListBackups) {
    List-Backups
    exit
}

if ([string]::IsNullOrEmpty($BackupDir)) {
    Write-Host "错误: 必须指定备份目录名称或使用 -ListBackups 查看可用备份。" -ForegroundColor Red
    Write-Host "使用 -Help 查看使用说明。" -ForegroundColor Yellow
    exit 1
}

Restore-Backup -BackupName $BackupDir